package com.meego.common.memcache.xmem;
import com.google.protobuf.GeneratedMessage;
import net.rubyeye.xmemcached.transcoders.BaseSerializingTranscoder;
import net.rubyeye.xmemcached.transcoders.CachedData;
import net.rubyeye.xmemcached.transcoders.Transcoder;
import net.rubyeye.xmemcached.transcoders.TranscoderUtils;

public class ProtosCoder extends BaseSerializingTranscoder implements Transcoder<GeneratedMessage> {

    private boolean primitiveAsString;
    public static final int SERIALIZED = 1;
    public static final int COMPRESSED = 2;
    public static final int SPECIAL_MASK = 65280;
    public static final int SPECIAL_BOOLEAN = 256;
    public static final int SPECIAL_INT = 512;
    public static final int SPECIAL_LONG = 768;
    public static final int SPECIAL_DATE = 1024;
    public static final int SPECIAL_BYTE = 1280;
    public static final int SPECIAL_FLOAT = 1536;
    public static final int SPECIAL_DOUBLE = 1792;
    public static final int SPECIAL_BYTEARRAY = 2048;
    private final TranscoderUtils transcoderUtils = new TranscoderUtils(true);
    private static ProtosHandler handler;

    public CachedData encode(GeneratedMessage obj) {
        CachedData data = new CachedData(0, obj.toByteArray());
        return data;
    }

    public GeneratedMessage decode(CachedData cdata) {
        try {
            return handler.decode(cdata.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setPackZeros(boolean packZeros) {
        System.out.println("setPackZeros");
        this.transcoderUtils.setPackZeros(packZeros);
    }

    public void setPrimitiveAsString(boolean primitiveAsString) {
        System.out.println("setPrimitiveAsString");
        this.primitiveAsString = primitiveAsString;
    }

    public TranscoderUtils getTranscoderUtils() {
        System.out.println("getTranscoderUtils");
        return this.transcoderUtils;
    }

    public boolean isPackZeros() {
        System.out.println("isPackZeros");
        return this.transcoderUtils.isPackZeros();
    }

    public boolean isPrimitiveAsString() {
        System.out.println("isPrimitiveAsString");
        return this.primitiveAsString;
    }
}
